package com.jt.www.util;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.font.FontProvider;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class Html2PdfUtil {
    public static final String LINUX_FONTS = "/usr/share/fonts/simsun/";

    public static void main(String[] args) throws Exception {
        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new DefaultFontProvider();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        fontProvider.addDirectory(classLoader.getResource("characters").getPath()); // 自定义字体路径、解决中文,可先用绝对路径测试。
        properties.setFontProvider(fontProvider);
        HtmlConverter.convertToPdf(new File("E:\\pdf\\ins-sd-linyi.html"), new File("E:\\testPdf.pdf"), properties);
    }

    public static byte[] convert(String html) throws IOException {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ConverterProperties props = new ConverterProperties();

        FontProvider fontProvider = new DefaultFontProvider();
        if (getOS()) {
            fontProvider.addDirectory(LINUX_FONTS);            //添加路径而不是添加字体
        } else {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            fontProvider.addDirectory(classLoader.getResource("characters").getPath()); // 自定义字体路径、解决中文,可先用绝对路径测试。
        }
        props.setFontProvider(fontProvider);

        HtmlConverter.convertToPdf(html, outputStream, props); // 无法灵活设置页边距等
        byte[] result = outputStream.toByteArray();
        outputStream.close();
        return result;
    }

    /**
     * 判断当前操作系统是否为linux
     *
     * @return
     */
    private static Boolean getOS() {
        String OS = System.getProperty("os.name").toLowerCase();
        return OS.indexOf("linux") >= 0;
    }


    /**
     * 带图片(章)
     *
     * @param html
     * @param images
     * @return
     * @throws IOException
     */
    public static byte[] html2Pdf(String html, List<Image> images) throws IOException {
        ConverterProperties props = new ConverterProperties();

        FontProvider fontProvider = new DefaultFontProvider();
        if (getOS()) {
            fontProvider.addDirectory(LINUX_FONTS);            //添加路径而不是添加字体
        } else {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            fontProvider.addDirectory(classLoader.getResource("characters").getPath()); // 自定义字体路径、解决中文,可先用绝对路径测试。
        }
        props.setFontProvider(fontProvider);


        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(bao);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = HtmlConverter.convertToDocument(html, pdf, props);

        Paragraph paragraph = new Paragraph();
        paragraph.addAll(images);
        document.add(paragraph);
        document.getRenderer().close();
        document.close();
        return bao.toByteArray();
    }
}
